package com.dao.impl;

import com.bean.Power;
import com.bean.Role;
import com.dao.PowerDao;
import com.util.JDBC_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PowerDaoImpl implements PowerDao {

    Connection conn = null;
    ResultSet rs= null;
    PreparedStatement ps = null;

    @Override
    public List<Power> getAllPower() {
        conn = JDBC_Util.getConnection();
        List<Power> list = new ArrayList<>();

        String sql = "select id, power_name from t_power";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String powerName = rs.getString("power_name");

                Power power = new Power();
                power.setId(id);
                power.setPowerName(powerName);
                list.add(power);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }

    @Override
    public List<Power> getPower(Role role) {
        int rId = role.getId();

        conn = JDBC_Util.getConnection();
        List<Power> list = new ArrayList<>();

        String sql = "select t_power.id, power_name from t_power" +
                " inner join t_role_power trp on t_power.id = trp.power_id" +
                " where role_id = ?";

        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,rId);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String powerName = rs.getString("power_name");

                Power power = new Power();
                power.setId(id);
                power.setPowerName(powerName);
                list.add(power);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }

    @Override
    public List<Power> getPower(String roleName) {

        conn = JDBC_Util.getConnection();
        List<Power> list = new ArrayList<>();

        String sql = "select t_power.id, power_name from t_power" +
                " inner join t_role_power trp on t_power.id = trp.power_id" +
                " inner join t_role tr on trp.role_id = tr.id" +
                " where role_name = ?";

        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,roleName);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String powerName = rs.getString("power_name");

                Power power = new Power();
                power.setId(id);
                power.setPowerName(powerName);
                list.add(power);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }
}
